package service

import (
	"admin100/app/dao"
	"admin100/app/model"
	"admin100/app/model/commonmodel"
	"admin100/library/define"
	"admin100/library/define/defErr"
)

var Version = &versionService{}

type versionService struct{}

func (s *versionService) FetchBranchPath(req *commonmodel.PageInfo) ([]*model.BranchPath, int) {
	var columns = dao.BranchPath.Columns

	m := dao.BranchPath
	m1 := m.Clone()

	data, err := m.Page(req.Cur, req.PNum).Order(columns.Id + " asc").FindAll()
	defErr.SelectRetPanic(err, data, define.DataOptFail)

	total, err := m1.Count()
	if err != nil {
		defErr.Panic(define.DataOptFail, err.Error())
	}

	return data, total
}

func (s *versionService) FetchPlatformVerChange(req *commonmodel.PageInfo) ([]*model.PlatformVersionPath, int) {
	var columns = dao.PlatformVersionPath.Columns

	m := dao.PlatformVersionPath
	m1 := m.Clone()

	data, err := m.Page(req.Cur, req.PNum).Order(columns.Id + " asc").FindAll()
	defErr.SelectRetPanic(err, data, define.DataOptFail)

	total, err := m1.Count()
	if err != nil {
		defErr.Panic(define.DataOptFail, err.Error())
	}

	return data, total
}

func (s *versionService) FetchVersionToolPath(req *commonmodel.PageInfo) ([]*model.VersionToolChange, int) {
	var columns = dao.VersionToolChange.Columns

	m := dao.VersionToolChange
	m1 := m.Clone()

	data, err := m.Page(req.Cur, req.PNum).Order(columns.Id + " asc").FindAll()
	defErr.SelectRetPanic(err, data, define.DataOptFail)

	total, err := m1.Count()
	if err != nil {
		defErr.Panic(define.DataOptFail, err.Error())
	}

	return data, total
}

// ver branch path edit
func (s *versionService) BranchPathEidt(req *model.BranchPath) {
	if req.Id > 0 {
		//更新
		data, err := dao.BranchPath.FindOne(dao.BranchPath.Columns.Id, req.Id)
		defErr.SelectRetPanic(err, data, define.DataOptFail)

	} else {
		//添加
		data, err := dao.BranchPath.FindOne(dao.BranchPath.Columns.Id, req.Id)
		if err != nil {
			defErr.Panic(define.DataOptFail, err.Error())
		}
		if data != nil {
			defErr.Panic(define.AccountExist)
		}
	}
	_, err := dao.BranchPath.Save(req)
	if err != nil {
		defErr.Panic(define.DataOptFail, err.Error())
	}
}

// platform version edit
func (s *versionService) PlatformVerPathEidt(req *model.PlatformVersionPath) {
	if req.Id > 0 {
		//更新
		data, err := dao.PlatformVersionPath.FindOne(dao.PlatformVersionPath.Columns.Id, req.Id)
		defErr.SelectRetPanic(err, data, define.DataOptFail)

	} else {
		//添加
		data, err := dao.PlatformVersionPath.FindOne(dao.PlatformVersionPath.Columns.Id, req.Id)
		if err != nil {
			defErr.Panic(define.DataOptFail, err.Error())
		}
		if data != nil {
			defErr.Panic(define.AccountExist)
		}
	}
	_, err := dao.PlatformVersionPath.Save(req)
	if err != nil {
		defErr.Panic(define.DataOptFail, err.Error())
	}
}

// ver tool edit
func (s *versionService) VerToolChangeEdit(req *model.VersionToolChange) {
	if req.Id > 0 {
		//更新
		data, err := dao.VersionToolChange.FindOne(dao.VersionToolChange.Columns.Id, req.Id)
		defErr.SelectRetPanic(err, data, define.DataOptFail)

	} else {
		//添加
		data, err := dao.VersionToolChange.FindOne(dao.VersionToolChange.Columns.Id, req.Id)
		if err != nil {
			defErr.Panic(define.DataOptFail, err.Error())
		}
		if data != nil {
			defErr.Panic(define.AccountExist)
		}
	}
	_, err := dao.VersionToolChange.Save(req)
	if err != nil {
		defErr.Panic(define.DataOptFail, err.Error())
	}
}
